package cn.rcth.base.serivce.impl;


import cn.rcth.base.VO.SMSConfig;
import cn.rcth.base.VO.VerifyCodeVO;
import cn.rcth.base.constants.BaiseConstants;
import cn.rcth.base.domain.VerifyCode;
import cn.rcth.base.enums.ErrorCode;
import cn.rcth.base.exception.CustomException;
import cn.rcth.base.serivce.ISendVerifyCodeService;
import cn.rcth.base.serivce.IVerifyCodeSerivce;
import cn.rcth.base.util.Assert;
import cn.rcth.base.util.DateUtil;
import cn.rcth.base.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class SendVerifyCodeServiceImpl implements ISendVerifyCodeService {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SMSConfig smsConfig;

    @Autowired
    private IVerifyCodeSerivce verifyCodeSerivce;


    @Override//验证法的发送
    public void sendVerifyCode(String phone) {
        //判断手机号
        Assert.isPhone(phone, ErrorCode.REGISTER_USERNAME_ERROR.getErrorMsg());
        //发送频率
        //获取验证码
        VerifyCode verifyCodes = verifyCodeSerivce.getCodeByPhone(phone);

        //String codes = BaiseConstants.verifyCode + phone;
        //VerifyCodeVO verifyCodeVO = (VerifyCodeVO) redisTemplate.opsForValue().get(codes);
        //创建时间
        Date date = new Date();
        //如果redis不为空的话开始计时
        if (verifyCodes != null) {
            Date sendTime = verifyCodes.getSendTime();
            //计算两个时间点的间隔秒数
            long secondsBetween = DateUtil.getSecondsBetween(date, sendTime)/60;
            boolean notCanSend = secondsBetween
                    > BaiseConstants.VERIFYCODE_SEND_FREQUENTLY_TIME;
            Assert.isFalse(notCanSend, ErrorCode.REGISTER_VERIFYCODE_INSUFFICIENT_QUANTITY_ERROR.getErrorMsg());
            //删除验证码
            verifyCodeSerivce.deleteCode(verifyCodes.getId());
        }
        //创建验证码
        String code = UUID.randomUUID().toString().substring(BaiseConstants.CODE_ONE, BaiseConstants.CODE_FOUR);
        //执行发送
        //send(phone, BaiseConstants.context + code);
        System.out.println("您的验证码为:" + code);
        //保存验证码到数据库
        //VerifyCodeVO verifyCode = new VerifyCodeVO();
        VerifyCode verifyCode = new VerifyCode();
        verifyCode.setSendTime(date);
        verifyCode.setCode(code);
        verifyCode.setPhone(phone);
        verifyCodeSerivce.save(verifyCode);
        //保存验证码到redis
       // redisTemplate.opsForValue().set(BaiseConstants.verifyCode+phone, verifyCode);
    }

    public void send(String phone, String context) {
        Map<String, String> params = new HashMap<>();
        params.put("Uid", smsConfig.getUid());
        params.put("Key", smsConfig.getKey());
        params.put("smsMob", phone);
        params.put("smsText", context);
        System.out.println(params.values());
        System.out.println(params);
        String result = null;
        try {
            HttpUtil.sendHttpRequest(smsConfig.getUrl(), params);
        } catch (Exception e) {
            throw new CustomException(ErrorCode.REGISTER_VERIFYCODE_ERROR.getErrorMsg());
        }
    }
}
